package dao.impl;

import java.sql.ResultSet;
import java.util.List;

import dao.BaseDao;
import dao.Mapper;
import dao.NewsDao;
import entity.News;

public class NewsDaoImpl extends BaseDao implements NewsDao {

	private Mapper<News> mapper = new Mapper<News>() {

		@Override
		public News doMapper(ResultSet rs) throws Exception {
			News n = new News();
			n.setId(rs.getInt("id"));
			n.setTime(rs.getDate("time"));
			n.setTitle(rs.getString("title"));
			return n;
		}
	};

	@Override
	public List<News> findAll() {
		return query("select * from news order by time desc", mapper);
	}

	@Override
	public int saveNews(News news) {
		String sql = "insert into news (title,time) values (?,?)";
		return update(sql , news.getTitle(),news.getTime());
	}

	@Override
	public int deleteNews(int id) {
		String sql = "delete from news where id = ?";
		return update(sql ,id);
	}

	@Override
	public News findById(int id) {
		return queryOne("select * from news where id = ?", mapper, id);
	}

	@Override
	public int updateNews(News news) {
		return update("update news set title = ? where id= ?", news.getTitle(),news.getId());
	}

}
